#include "ns3/assert.h"
#include "ns3/log.h"
#include "ns3/header.h"
#include "ns3/simulator.h"
#include "ac_FileHeader.h"
#include <vector>

NS_LOG_COMPONENT_DEFINE ("AcFileHeader");

namespace ns3 {


NS_OBJECT_ENSURE_REGISTERED (AcFileHeader);
AcFileHeader::AcFileHeader()
{
NS_LOG_FUNCTION (this);
}


AcFileHeader::AcFileHeader (ACFile file):localFile(file)
{
  NS_LOG_FUNCTION (this);
}

/*
void
SeqTsHeader::SetSeq (uint32_t seq)
{
  NS_LOG_FUNCTION (this << seq);
  m_seq = seq;
}
uint32_t
SeqTsHeader::GetSeq (void) const
{
  NS_LOG_FUNCTION (this);
  return m_seq;
}

Time
SeqTsHeader::GetTs (void) const
{
  NS_LOG_FUNCTION (this);
  return TimeStep (m_ts);
}
*/

ACFile AcFileHeader::GetData()
{

return localFile;
}

TypeId
AcFileHeader::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::AcFileHeader")
    .SetParent<Trailer> ()
    .AddConstructor<AcFileHeader> ()
  ;
  return tid;   
}
TypeId
AcFileHeader::GetInstanceTypeId (void) const
{
  return GetTypeId ();
}
void
AcFileHeader::Print (std::ostream &os) const
{
  NS_LOG_FUNCTION (this << &os);
 // os << "(seq=" << m_seq << " time=" << TimeStep (m_ts).GetSeconds () << ")";
}
    
uint32_t
AcFileHeader::GetSerializedSize (void) const
{
  NS_LOG_FUNCTION (this);
uint32_t strSize=localFile.file_name.length();
  uint32_t fSize=localFile.data.GetSize();
  


  return 4+4+strSize+fSize;
}

void
AcFileHeader::Serialize (Buffer::Iterator start) const
{
  NS_LOG_FUNCTION (this << &start);
Buffer::Iterator j = start;

j.Prev(GetSerializedSize());
// Invia info frame information size
j.WriteU32(localFile.size);

j.WriteU32((localFile.file_name).length());
 
for ( unsigned i=0; i<(localFile.file_name).length(); i++) 
j.WriteU8((uint8_t)((localFile.file_name).at(i)));

j.Write((localFile.data).Begin(),(localFile.data).End()); 


}



uint32_t
AcFileHeader::Deserialize (Buffer::Iterator start)
{
  NS_LOG_FUNCTION (this << &start);

  Buffer::Iterator j = start;

while(!j.IsStart())
j.Prev();


localFile.size=j.ReadU32();
uint32_t stringLength=j.ReadU32();
  


for ( unsigned i=0; i<stringLength; i++) 
{
localFile.file_name+=(char)j.ReadU8();

}

localFile.data.AddAtEnd(localFile.size);
Buffer::Iterator i=localFile.data.Begin();

while(!j.IsEnd())
{

i.WriteU8(j.ReadU8());

}  




  return GetSerializedSize ();
}

} // namespace ns3
